<script>
	// 本地iconf.css,暂时先在线链接
	import 'static/iconfont/iconfont.css';
	export default {
		globalData: {  
			opid: '',
			username:'',
			userimg:''
		},  
		onLaunch: function() {
			// console.log('App Launch')
			this.getSetting();
		},
		onShow: function() {
			// console.log('App Show')
		},
		onHide: function() {
			// console.log('App Hide')
		},
		methods:{
			//是否授权
			getSetting(){
				var self=this;
				// 查看是否授权
				wx.getSetting({
				  success (res){
				    if (res.authSetting['scope.userInfo']) {
				      // 已经授权
					  self.getUserInfo();
				    }else{
						console.log('未授权')
					}
				  }
				})
			},
			//已授权
			getUserInfo(){
				var self=this;
				uni.login({
					provider: 'weixin',
					scopes: 'auth_base',
					success: res => {
						uni.getUserInfo({
							provider: 'weixin',
							success: infoRes => {
								// console.log(infoRes.rawData);
								//公众号内生成的appid和secret
								var appid = 'wxe34bc6731f7ed51f';
								var secret = '9419e5de0b8ea314a7d0b04e92a4a270';
								var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' +
									res.code + '&grant_type=authorization_code';
								uni.request({
									url: l,
									method: 'GET',
									success: res => {
										self.savaUser(infoRes.rawData,res.data.openid);
										getApp().globalData.opid=res.data.openid;
									}
								});
							}
						});
					},
					fail: err => {
						console.error('授权登录失败：' + JSON.stringify(err));
					}
				})
			},
			// 用户授权信息和openid存入数据库
			savaUser(udata,uid){
				uni.request({
					url: this.zurl + '/user/userLogin',
					method: 'POST',
					data: {
						"rawData": udata,
						"openid": uid
					},
					success: res => {
						if(res.data.code==0){
							// console.log(res);
							console.log('保存了用户信息')
							getApp().globalData.userimg=res.data.data.userAvatarurl;
							getApp().globalData.username=res.data.data.userNickname;
							// uni.showToast({
							// 	title: res.data.msg,
							// });
						}
					}
				});
			}
			
			
			
			
		}
		
	}
</script>

<style lang="less">
	/* 在线链接无法在微信端编译 */
	/* @import url("https://at.alicdn.com/t/font_1292944_ypzb3jvx1b.css"); */
	/* uni-app样式 */
	/* @import './common/uni.css'; */
	
	/*每个页面公共css */
/* homepage start */
/*九宫格滑动点start */
.homegrid{
 .uni-swiper-dots-horizontal{
	bottom: 0;
}
/* 文字大小 */
text{
	font-size: 12px;
}
/* 点 */
.uni-swiper-dot-active{
	background-color: #f94f47;
}
.uni-swiper-dot{
	height: 2px;
	border-radius: inherit;
}
}
/*九宫格滑动点end */
/* homepage end */
</style>
